package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.GoodTypeDto;
import io.geekidea.boot.shop.entity.GoodType;
import io.geekidea.boot.shop.mapper.GoodTypeMapper;
import io.geekidea.boot.shop.query.app.AppGoodTypeQuery;
import io.geekidea.boot.shop.query.web.GoodTypeQuery;
import io.geekidea.boot.shop.service.base.api.GoodTypeService;
import io.geekidea.boot.shop.vo.app.AppGoodTypeVo;
import io.geekidea.boot.shop.vo.web.GoodTypeVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class GoodTypeServiceImpl extends ServiceImpl<GoodTypeMapper, GoodType> implements GoodTypeService {

    @Autowired
    private GoodTypeMapper goodTypeMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addGoodType(GoodTypeDto dto) {
        GoodType goodType = new GoodType();
        BeanUtils.copyProperties(dto, goodType);
        return save(goodType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateGoodType(GoodTypeDto dto) {
        Integer id = dto.getId();
        GoodType goodType = getById(id);
        if (goodType == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, goodType);
        return updateById(goodType);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteGoodType(Integer id) {
        return removeById(id);
    }

    @Override
    public GoodTypeVo getGoodTypeById(Integer id) {
        return goodTypeMapper.getGoodTypeById(id);
    }

    @Override
    public Paging<GoodTypeVo> getGoodTypePage(GoodTypeQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<GoodTypeVo> list = goodTypeMapper.getGoodTypePage(query);
        Paging<GoodTypeVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppGoodTypeVo getAppGoodTypeById(Integer id) {
        return goodTypeMapper.getAppGoodTypeById(id);
    }

    @Override
    public Paging<AppGoodTypeVo> getAppGoodTypePage(AppGoodTypeQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppGoodTypeVo> list = goodTypeMapper.getAppGoodTypePage(query);
        Paging<AppGoodTypeVo> paging = new Paging<>(list);
        return paging;
    }

}
